<template>
  <div class="container">
    <data-view
      propViewKey="base_matr_edit"
      propViewForm="base/material/matr-edit/detail.vue"
      :propShowToolBar="true"
      :propNoEditRow="true"
      :propParam="getPropParam()"
      @extra-button-click="extraButtonClick"
      :propIsTdesign="true"
      :propAutoFetchData="false"
      :propOnlyQueryAll="cpOnlyQuery2"
      ref="dataView"
      :propUserParam="cpPropUserParam"
    />

    <!--导入拆分商品-->
    <customDrawer title="导入商品" ref="customDrawerImportDataCustom"
                  :widthPx="600" :heightPx="500"
                  hideConfirm
    >
      <template #content>
        <dataImportArea
          downloadPath="/base/matr/excelDownloadTempForDataImport"
          importPath="/base/matr/excelImportForDataImport"
          :sheetName="'商品'"
          helpMsg="
          1、【下载模板】维护好数据 -> 【选择文件】导入数据<br>
          2、<span class='span-msg-nopad'>供应商不存在的忽略</span><br>
          3、<span class='span-msg-nopad'>厂商、分类会自动创建</span><br>
          4、<span class='span-msg-nopad'>去重规则：商品名称、供应商名称、商品规格，如果找到匹配的则忽略</span><br>
          5、<span class='span-msg-nopad'>新增的商品将默认禁用,高耗设置为标签管理寄售，低耗设置为非标签管理寄售，检查没问题后再启用</span><br>
          6、<span class='span-msg-nopad'>导入成功后将根据医保代码27位匹配并绑定HIS相关收费项目</span><br>
          7、<span class='span-msg-nopad'>导入成功后将自动过滤当前列表为导入后的数据</span><br>
"
          @success="dataImportSuccess"
          ref="dataImportAreaImportDataCustom"
        />
      </template>
    </customDrawer>


    <!--导入拆分商品-->
    <customDrawer title="导入拆分商品" ref="customDrawerImportDataSplit"
                  :widthPx="500" :heightPx="500"
                  hideConfirm
    >
      <template #content>
        <dataImportArea
          downloadPath="/base/matr/excelDownloadTempForDataSplit"
          importPath="/base/matr/excelImportForDataSplit"
          :sheetName="'商品拆分'"
          helpMsg="
          1、【下载模板】维护好数据 -> 【选择文件】导入数据<br>
          2、<span class='span-msg-nopad'>创建新的商品并赋予该供应商</span><br>
          3、<span class='span-msg-nopad'>供应商不存在的忽略</span><br>
          4、<span class='span-msg-nopad'>按照品名品规未找到商品的忽略</span><br>
          5、<span class='span-msg-nopad'>该供应商存在同品名、品规的忽略</span><br>
          6、<span class='span-msg-nopad'>新增的商品将默认启用</span><br>
"
          @success="menuActiveCall"
          ref="dataImportAreaImportDataSplit"
        />
      </template>
    </customDrawer>

      <!--批量设置属性-->
    <setBatchWin
      ref="setBatchWin"
      @afterSet="menuActiveCall"
      setType="商品"
      setAction="base/matr/setBatch"
    >
      <template #form>
        <customFormItem
          label="供方名称" name="supplierName" :parent="_self" :form="$refs.setBatchWin.setForm"
          @focusTo="$refs.supplierName.focus()"
          :type="constCommon.formItemType.slot.name"
          full-width
        >
          <template #content>
            <spSelect
              v-model="$refs.setBatchWin.setForm.supplierId"
              :valueDisplay="$refs.setBatchWin.setForm.supplierName"
              @change="spSelect"
              ref="supplierName"
            />
            <customButton
              title="+"
              help="列表不存在供应商时，可以新增供应商"
              theme="primary"
              size="small"
              @click="utilSelect.openSelectCreateFormSp(spSelect)"
              style="font-size: 20px; width: 20px;position: absolute;right: -22px;"
            >
            </customButton>
          </template>
        </customFormItem>


        <customFormItem
          label="生产厂商" name="mfgName" :parent="_self" :form="$refs.setBatchWin.setForm"
          @focusTo="$refs.mfgName.focus()"
          :type="constCommon.formItemType.slot.name"
          full-width
        >
          <template #content>
            <mfgSelect
              v-model="$refs.setBatchWin.setForm.mfgId"
              :valueDisplay="$refs.setBatchWin.setForm.mfgName"
              @change="mfgSelect"
              ref="mfgName"
            />
            <customButton
              title="+"
              help="列表不存在厂商时，可以新增厂商"
              theme="primary"
              size="small"
              @click="utilSelect.openSelectCreateFormMfg(mfgSelect)"
              style="font-size: 20px; width: 20px;position: absolute;right: -22px;"
            >
            </customButton>
          </template>
        </customFormItem>

        <customFormItem label="商品分类" name="baseType" :parent="_self" :form="$refs.setBatchWin.setForm"
                        :type="constCommon.formItemType.selectDict.name"
                        :config="{dictType: constCommon.dictType.baseMatrType}"
                        full-width
        />

        <customFormItem label="是否寄售" name="flagConsignment" :parent="_self" :form="$refs.setBatchWin.setForm"
                        :type="constCommon.formItemType.selectStatic.name"
                        :config="{options: constCommon.nobusConst.yesNoBoolean}"
                        full-width
        />
        <customFormItem label="标签管理" name="flagOmoc" :parent="_self" :form="$refs.setBatchWin.setForm"
                        :type="constCommon.formItemType.selectStatic.name"
                        :config="{options: constCommon.nobusConst.yesNoBoolean}"
                        full-width
        />
        <customFormItem label="常备耗材" name="flagCenterPurchase" :parent="_self" :form="$refs.setBatchWin.setForm"
                        :type="constCommon.formItemType.selectStatic.name"
                        :config="{options: constCommon.nobusConst.yesNoBoolean}"
                        full-width
        />
        <customFormItem label="禁用" name="flagDisabled" :parent="_self" :form="$refs.setBatchWin.setForm"
                        :type="constCommon.formItemType.selectStatic.name"
                        :config="{options: constCommon.nobusConst.yesNoBoolean}"
                        full-width
        />
      </template>
    </setBatchWin>
  </div>
</template>

<script>
import dataImportArea from '@/components/custom/bus/dataImportArea.vue'
import customUpload from '@/components/custom/form/customUpload.vue'
import setBatchWin from '@/components/custom/tool/setBatchWin.vue'
import spSelect from '@/components/custom/bus/select/spSelect.vue'
import mfgSelect from '@/components/custom/bus/select/mfgSelect.vue'

export default {
  name: 'matr',
  props: ['propParameter'],
  data() {
    return {
      // 列表选择的数据
      dataRowList: [],
      setRules: {
        setField: [{ required: true, message: '必填', trigger: 'blur' }],
        setValue: [{ required: true, message: '必填', trigger: 'blur' }]
      },
    }
  },
  components: { mfgSelect, spSelect, customUpload, dataImportArea,setBatchWin },
  mounted() {
    this.menuActiveCall()
    if (this.cpPropUserParam.queryType == 'crud') {
      this.utilMessage.warning('禁用、HIS禁用、外网禁用的商品以红色标记')
    }
  },
  methods: {
    menuActiveCall() {
      this.$refs.dataView.setQueryWrapper(this.getPropPreQueryWrapper())
    },
    extraButtonClick(para) {
      let {key: buttonKey} = para.button
      if (buttonKey == 'pushOnce') {
        //客户端操作
        let idList = para.data.map(o => o.id)
        if (idList instanceof Array) {
          if (idList.length == 0) {
            this.utilMessage.error('请先勾选列表需要操作的商品')
            return false
          }
        }
        this.utilMessage.confirm('是否推送该所选耗材到供应商平台', () => {
          this.utilVue.loadShow(true)
          this.$ajax.post('base/matr/getOnceMatrMscm', idList).then(response => {
            this.utilVue.loadHide()
            if (response.code == 0) {
              this.utilMessage.success(response.msg)
              this.menuActiveCall()
            } else {
              this.utilMessage.error(response.msg)
            }
          })
        })
      } else  if (buttonKey == 'viewAdjust') {
        this.utilSelect.openReportAdjust(para.data.id)
      } else  if (buttonKey == 'importDataSplit') {
        this.$refs.dataImportAreaImportDataSplit.clear()
        this.$refs.customDrawerImportDataSplit.show()
      } else  if (buttonKey == 'setBatch') {
        this.$refs.setBatchWin.show(para.data)
      } else  if (buttonKey == 'importDataCustom') { //导入商品
        this.$refs.dataImportAreaImportDataCustom.clear()
        this.$refs.customDrawerImportDataCustom.show()
      }
    },
    getPropPreQueryWrapper() {
      let wrapper = this.$newWrapper()
      //筛选耗材类型
      switch (this.utils.getMtrBaseType(this)) {
      case 'reagent': //试剂
        //wrapper.eq('base_type', 'reagent')
        break
      case 'drugs': //药品
        //wrapper.eq('base_type', 'drugs')
        break
      case 'matr': //药品
        //wrapper.notIn('base_type', ['reagent', 'drugs'])
        break
      default:
        break
      }
      // let obFromUrl = this.utils.getObFromUrl(this.propParameter)
      // if (obFromUrl.flagUsed) {
      //   // wrapper.ne('flag_disabled', 1)
      //   // wrapper.ne('his_disabled', 1)
      //   // wrapper.ne('hrp_finance_disabled', 1)
      //   // wrapper.ne('hrp_disabled', 1)
      //   // wrapper.ne('flag_disabled_sp', 1)
      //   // this.utilMessage.warning('当前列表仅显示正常流通的耗材')
      // }
      return wrapper
    },
    getPropParam() {
      return {matrBaseType: this.utils.getMtrBaseType(this)}
    },

    mfgSelect(row) {
      this.$refs.setBatchWin.setFormValue('mfgId', row.id)
      this.$refs.setBatchWin.setFormValue('mfgName', row.name)
    },
    spSelect(row) {
      this.$refs.setBatchWin.setFormValue('supplierId', row.id)
      this.$refs.setBatchWin.setFormValue('supplierName', row.name)
    },

    // 商品导入成功后
    dataImportSuccess(data, resp) {
      let wrapper = this.$newWrapper()
      this.$refs.dataView.setQueryForm('remark',data.tms)
      // this.utilMessage.info('已为您查询本次导入的数据')
    }
  },
  computed: {
    // 是否只读
    cpOnlyQuery2() {
      let obFromUrl = this.utils.getObFromUrl(this.propParameter)
      return obFromUrl.readonly != undefined && obFromUrl.readonly == 1
    },
    cpPropUserParam() {
      let param = {
        queryType: 'crud'
      }
      let obFromUrl = this.utils.getObFromUrl(this.propParameter)
      if (obFromUrl.flagUsed) {
        param.queryType = 'useIn'
      }
      return param
    }
  }
}
</script>

<style lang="scss">
  .container {
    height: 100%;
    background-color: #fff;
  }
  div.vxe-cell.c--tooltip {
    white-space: pre !important;
  }
</style>
